home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 201-225 / disk_218 / edlib / getopt.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  2KB  |  75 lines

  1. /*
  2.  * edlib v1.1 Copyright 1989 Edwin Hoogerbeets
  3.  * This code is freely redistributable as long as no charge other than
  4.  * reasonable copying fees are levied for it.
  5.  */
  6.  
  7. #define NULL    0
  8. #define EOF     (-1)
  9. #define ERR(s, c)       if(opterr){\
  10.         extern int strlen(), write();\
  11.         char errbuf[2];\
  12.         errbuf[0] = c; errbuf[1] = '\n';\
  13.         (void) write(2, argv[0], (unsigned)strlen(argv[0]));\
  14.         (void) write(2, s, (unsigned)strlen(s));\
  15.         (void) write(2, errbuf, 2);}
  16.  
  17. extern int strcmp();
  18. extern char *strchr();
  19.  
  20. int     opterr = 1;
  21. int     optind = 1;
  22. int     optopt;
  23. char    *optarg;
  24.  
  25. int
  26. getopt(argc, argv, opts)
  27. register int     argc;
  28. char    **argv, *opts;
  29. {
  30.         static int sp = 1;
  31.         register int c;
  32.         register char *cp;
  33.  
  34.         if(sp == 1)
  35.                 if(optind >= argc)
  36.                         return(EOF);
  37.                 else {
  38.                     if(argv[optind][0] != '-') {
  39.                         optarg = argv[optind++];
  40.                         return(NULL);
  41.                     }
  42.                     if(strcmp(argv[optind], "--") == NULL) {
  43.                         optind++;
  44.                         return(EOF);
  45.                     }
  46.                 }
  47.         optopt = c = argv[optind][sp];
  48.         if(c == ':' || (cp=strchr(opts, c)) == NULL) {
  49.                 ERR(": illegal option -- ", c);
  50.                 if(argv[optind][++sp] == '\0') {
  51.                         optind++;
  52.                         sp = 1;
  53.                 }
  54.                 return('?');
  55.         }
  56.         if(*++cp == ':') {
  57.                 if(argv[optind][sp+1] != '\0')
  58.                         optarg = &argv[optind++][sp+1];
  59.                 else if(++optind >= argc) {
  60.                         ERR(": option requires an argument -- ", c);
  61.                         sp = 1;
  62.                         return('?');
  63.                 } else
  64.                         optarg = argv[optind++];
  65.                 sp = 1;
  66.         } else {
  67.                 if(argv[optind][++sp] == '\0') {
  68.                         sp = 1;
  69.                         optind++;
  70.                 }
  71.                 optarg = NULL;
  72.         }
  73.         return(c);
  74. }
  75.